package com.ruoyi.crm.sale.controller;

import java.util.List;
import java.util.Map;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.crm.sale.domain.CrmBusiness;
import com.ruoyi.crm.sale.domain.CrmBusinessProduct;
import com.ruoyi.crm.sale.service.ICrmBusinessService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商机Controller
 * 
 * @author ruoyi
 * @date 2021-09-09
 */
@RestController
@RequestMapping("/crm/business")
public class CrmBusinessController extends BaseController
{
    @Autowired
    private ICrmBusinessService crmBusinessService;

    /**
     * 查询商机列表
     */
    @PreAuthorize("@ss.hasPermi('crm:business:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmBusiness crmBusiness)
    {
        startPage();
        List<CrmBusiness> list = crmBusinessService.selectCrmBusinessList(crmBusiness);
        return getDataTable(list);
    }

    /**
     * 导出商机列表
     */
    @PreAuthorize("@ss.hasPermi('crm:business:export')")
    @Log(title = "商机", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CrmBusiness crmBusiness)
    {
        List<CrmBusiness> list = crmBusinessService.selectCrmBusinessList(crmBusiness);
        ExcelUtil<CrmBusiness> util = new ExcelUtil<CrmBusiness>(CrmBusiness.class);
        return util.exportExcel(list, "商机数据");
    }

    /**
     * 获取商机详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:business:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(crmBusinessService.selectCrmBusinessById(id));
    }

    /**
     * 获取商机对应的产品信息
     */
    @PreAuthorize("@ss.hasPermi('crm:business:query')")
    @GetMapping(value = "/getCrmBusProductById")
    public AjaxResult getCrmBusProductById(CrmBusinessProduct busPro)
    {
        return AjaxResult.success(crmBusinessService.getCrmBusProductById(busPro));
    }

    /**
     * 新增商机
     */
    @PreAuthorize("@ss.hasPermi('crm:business:add')")
    @Log(title = "商机", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmBusiness crmBusiness)
    {
        return toAjax(crmBusinessService.insertCrmBusiness(crmBusiness));
    }

    /**
     * 修改商机
     */
    @PreAuthorize("@ss.hasPermi('crm:business:edit')")
    @Log(title = "商机", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmBusiness crmBusiness)
    {
        return toAjax(crmBusinessService.updateCrmBusiness(crmBusiness));
    }

    /**
     * 删除商机
     */
    @PreAuthorize("@ss.hasPermi('crm:business:remove')")
    @Log(title = "商机", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(crmBusinessService.deleteCrmBusinessByIds(ids));
    }

    /**
     * 修改商机阶段
     */
    @PreAuthorize("@ss.hasPermi('crm:business:edit')")
    @Log(title = "商机", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/updateBusinessStage")
    public AjaxResult updateBusinessStage(@RequestBody CrmBusiness crmBusiness)
    {
    	crmBusinessService.updateBusinessStage(crmBusiness);
    	return AjaxResult.success(crmBusinessService.selectCrmBusinessById(crmBusiness.getId()));
    }

    /**
     * 查询有效商机数量
     */
    @PreAuthorize("@ss.hasPermi('crm:business:list')")
    @Log(title = "查询有效商机数量", businessType = BusinessType.OTHER)
    @PostMapping("/selBusCount")
    public AjaxResult selBusCount(@RequestBody Map<String, Object> params)
    {
        return AjaxResult.success(crmBusinessService.selBusCount(params));
    }

    /**
     *商机阶段销售漏斗 
     */
    @Log(title = "商机阶段销售漏斗 ", businessType = BusinessType.OTHER)
    @PostMapping("/funnelCount")
    public AjaxResult funnelCount(@RequestBody Map<String, Object> params)
    {
        return AjaxResult.success(crmBusinessService.funnelCount(params));
    }
}
